package com.groupseven.backend.controller;


import com.groupseven.backend.domain.Patent.Patent;
import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.services.PatentService;
import com.groupseven.backend.services.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/Patents")
public class PatentController {
    @Autowired
    private UserService userService;
    @Autowired
    private PatentService patentService;

    @GetMapping("/")
    public ResultType getallPatents(){
        List<Patent> tmp =patentService.getallPatents();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPatents(tmp);
        return resultType;
    }

    @GetMapping("/ways1/{PatentNumber}")
    public ResultType getPatentByPatentNumber(@PathVariable("PatentNumber") String PatentNumber){
        if(patentService.getPatentByPatentNumber(PatentNumber)==null){
            return new  ResultType(0,"查询失败,专利编号不存在",200);
        }
        else {
            Patent patent=patentService.getPatentByPatentNumber(PatentNumber);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPatent(patent);
            return resultType;
        }
    }

    @PostMapping("/")
    public ResultType AddPatentInfo(@RequestBody @Valid Patent patent){
        Subject subject= SecurityUtils.getSubject();
        User user=(User)subject.getPrincipal();
        patent.setUsername(user.Username);
        patent.setIsfinish(2);
        patent.setApprovalStatus(2);
        patent.setAskTime(new Date());
        /////////////////////////////////////////////
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss:SSS");
        patent.setPatentNumber(formatter.format(new Date()));
        /////////////////////////////////////////////

        Date date=new Date();
        patent.setStartTime(date);

        List<Patent> tmp = new ArrayList<>();
        tmp.add(patent);
        if(patentService.AddPatentInfo(patent)==0){
            return new  ResultType(0,"该专利编号已存在，插入失败",200);
        } else {
            return new ResultType(1,"插入成功",200);
        }
    }

    @PutMapping("/")
    public ResultType updatePatentInfo(@RequestBody @Valid Patent patent){
        if(patentService.updatePatentInfo(patent)==0){
            return new  ResultType(0,"该专利编号不存在，修改失败",200);
        } else if(patentService.updatePatentInfo(patent)==-2){
            return new  ResultType(0,"用户名不存在，修改失败",200);
        }
        else {
            return new ResultType(1,"修改成功",200);
        }
    }

    @DeleteMapping("/")
    public ResultType deleteallPatents(){
        if(patentService.deleteallPatents()==0){
            return new  ResultType(0,"删除失败",200);
        }
        else {
            return new  ResultType(1,"删除成功",200);
        }
    }

    @DeleteMapping("/ways1/{PatentNumber}")
    public ResultType deletePatentByPatentNumber(@PathVariable("PatentNumber") String PatentNumber){
        if(patentService.deletePatentByPatentNumber(PatentNumber)==0){
            return new  ResultType(0,"删除失败，专利编号不存在",200);
        }
        else {
            return new  ResultType(1,"删除成功",200);
        }
    }

    //////////////////////////////////////////////////////////////////////////////
    @PostMapping("/s/")
    public ResultType mutildelPapers(@RequestBody List<Patent> patents){
        List<String> PatentNumbers=new ArrayList<>();
        for(Patent patent:patents){
            PatentNumbers.add(patent.PatentNumber);
        }

        if(patentService.mutildelPatents(PatentNumbers)==0){
            return new ResultType(0,"删除失败",200);
        }
        else {
            return new ResultType(1,"删除成功",200);
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways1/{PatentNumber}")
    public ResultType getlikePatentByPatentNumber(@PathVariable("PatentNumber") String PatentNumber){
        if(patentService.getlikePatentByPatentNumber(PatentNumber).isEmpty()){
            return new  ResultType(0,"查询失败,专利编号不存在",200);
        }
        else {
            List<Patent> patents=patentService.getlikePatentByPatentNumber(PatentNumber);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPatents(patents);
            return resultType;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways2/{PatentName}")
    public ResultType getlikePatentByPatentName(@PathVariable("PatentName") String PatentName){
        if(patentService.getlikePatentByPatentName(PatentName).isEmpty()){
            return new  ResultType(0,"查询失败,专利名不存在",200);
        }
        else {
            List<Patent> patents=patentService.getlikePatentByPatentName(PatentName);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPatents(patents);
            return resultType;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways3/{username}")
    public ResultType getlikePatentByusername(@PathVariable("username") String username){
        if(patentService.getlikePatentByusername(username).isEmpty()){
            return new  ResultType(0,"查询失败,用户名不存在",200);
        }
        else {
            List<Patent> patents=patentService.getlikePatentByusername(username);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPatents(patents);
            return resultType;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways4/{PatentAllAuthors}")
    public ResultType getlikePatentByPatentAllAuthors(@PathVariable String PatentAllAuthors){
            List<Patent> patents=patentService.getlikePatentByPatentAllAuthors(PatentAllAuthors);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPatents(patents);
            return resultType;
    }
    //////////////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////
    @GetMapping("/search/{query}")
    public ResultType searchqueryPatent(@PathVariable String query){
        if(patentService.searchqueryPatent(query).isEmpty()){
            return new  ResultType(0,"查询失败,",200);
        }
        else {
            List<Patent> patents=patentService.searchqueryPatent(query);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPatents(patents);
            return resultType;
        }
    }

/////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////

    @GetMapping("/search/{username}/{query}")
    public ResultType searchuserqueryPatent(@PathVariable String query,@PathVariable String username){
        if(patentService.searchuserqueryPatent(query,username).isEmpty()){
            return new ResultType(0,"查询失败",200);
        }
        else {
            List<Patent> patents=patentService.searchuserqueryPatent(query,username);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPatents(patents);
            return resultType;
        }
    }
// ////////////////////////////////////////////////////////////////

}
